package com.hisan.freeride.home.activity;

import android.os.Bundle;
import android.view.View;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.base.BaseFragment;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.callback.NoDialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.utils.MyHandlerUtil;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.HomeReleaseBinding;
import com.hisan.freeride.home.adapter.ReleaseAdapter;
import com.hisan.freeride.home.model.Release;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/6
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：发布的行程页面
 */
public class ReleaseActivity extends BaseActivity<HomeReleaseBinding> {
    private ReleaseAdapter adapter;
    private int page=1;
    private int stuats=1;
    private int id;
    private List<Release> releases=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return  R.layout.home_release;
    }

    protected void initView() {
        mBinding.setStateModel(mStateModel);
        adapter=new ReleaseAdapter(new ArrayList<>(0));
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(this));
        mBinding.rv.setAdapter(adapter);
        adapter.setButtonClickListener((p, data) -> {
            if (!CollectionUtils.isNullOrEmpty(data)){
                if (p==1){
                    stuats=1;
                    id=data.getId();
                    getmDialog("是否取消当前行程?","取消","确定").show();
                }else if (p==2){
                    //查看用户预约列表
                    Bundle bundle=new Bundle();
                    bundle.putInt("id",data.getId());
                    startActivityForResult(LookReleaseActivity.class,99,bundle,true);
                }else if (p==3){
                    stuats=2;
                    id=data.getId();
                    getmDialog("是否删除当前行程?","取消","确定").show();
                }
            }
        });
    }

    @Override
    protected void initEvent() {
//        adapter.setOnItemClickListener(data -> {
//            Bundle bundle=new Bundle();
//            bundle.putInt("id",data.getId());
//            startActivityForResult(PushDetailsActivity.class,99,bundle,true);
//        });
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                releases.clear();
                loadData(true);
                mBinding.pull.finishRefresh();
            }

            @Override
            public void loadMore() {
                page++;
                loadData(true);
                mBinding.pull.finishLoadMore();
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
            OkGo.<List<Release>>get(AppConfig.Mytrip)
                    .tag(this)
                    .params("page",page)
                    .params("size",20)
                    .execute(new NoDialogCallback<List<Release>>(this) {
                        @Override
                        public void onSuccess(com.lzy.okgo.model.Response<List<Release>> response) {
                            super.onSuccess(response);
                            if (!CollectionUtils.isNullOrEmpty(response.body())){
                                if (isOk(response)){
                                    showData();
                                    if(response.body().size()>=20){
                                        mBinding.pull.setCanLoadMore(true);
                                    }else {
                                        mBinding.pull.setCanLoadMore(false);
                                    }
                                    releases.addAll(response.body());
                                    adapter.setNewData(releases);
                                }else {
                                    showErrorView(response.code());
                                }
                            }else {
                                if (!CollectionUtils.isNullOrEmpty(releases))
                                    adapter.setNewData(releases);
                                else   noData();
                            }
                        }
                        @Override
                        public void onError(com.lzy.okgo.model.Response<List<Release>> response) {
                            super.onError(response);
                            showErrorView(response.code());
                        }
                    });
    }



    //取消行程
    private void cancel(int id) {
        HttpParams Params=new HttpParams();
        Params.put("id",id);
        Params.put("type",1);
        OkGo.put(AppConfig.Tripcancel)
                .tag(this)
                .params(Params)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            page=1;
                            releases.clear();
                            showSuccessToast("取消成功");
                            MyHandlerUtil.getHandler().postDelayed(() -> loadData(true),200);
                        }else showError(response);
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }

    //删除行程
    private void Delete(int id) {
        OkGo.delete(AppConfig.Deltrip)
                .tag(this)
                .params("id",id)
                .params("type",0)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            page=1;
                            releases.clear();
                            showSuccessToast("删除成功");
                            MyHandlerUtil.getHandler().postDelayed(() -> loadData(true),200);
                        } else showError(response);
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });

    }



    @Override
    public void onOk() {
        if (stuats==1){
            cancel(id);
        }else {
            Delete(id);
        }
    }
}
